package org.fisco.bcos.transhash.contract;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.fisco.bcos.sdk.abi.FunctionReturnDecoder;
import org.fisco.bcos.sdk.abi.TypeReference;
import org.fisco.bcos.sdk.abi.datatypes.Bool;
import org.fisco.bcos.sdk.abi.datatypes.Function;
import org.fisco.bcos.sdk.abi.datatypes.Type;
import org.fisco.bcos.sdk.abi.datatypes.Utf8String;
import org.fisco.bcos.sdk.abi.datatypes.generated.tuples.generated.Tuple1;
import org.fisco.bcos.sdk.abi.datatypes.generated.tuples.generated.Tuple2;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.contract.Contract;
import org.fisco.bcos.sdk.crypto.CryptoSuite;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.model.CryptoType;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.model.callback.TransactionCallback;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;

@SuppressWarnings("unchecked")
public class Transhash extends Contract {
    public static final String[] BINARY_ARRAY = {"60806040526001600360006101000a81548160ff0219169083151502179055506000600360016101000a81548160ff0219169083151502179055506000600360026101000a81548160ff02191690831515021790555060006003806101000a81548160ff02191690831515021790555034801561007b57600080fd5b5061086c8061008b6000396000f300608060405260043610610078576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630293e5371461007d5780632c5cc9d0146100e65780635f2b24051461011557806372f1adda14610144578063907e2410146101f3578063e8e045ab14610283575b600080fd5b34801561008957600080fd5b506100e4600480360381019080803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919291929050505061037f565b005b3480156100f257600080fd5b506100fb6103e4565b604051808215151515815260200191505060405180910390f35b34801561012157600080fd5b5061012a6103fb565b604051808215151515815260200191505060405180910390f35b34801561015057600080fd5b506101f1600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290505050610411565b005b3480156101ff57600080fd5b5061020861048f565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561024857808201518184015260208101905061022d565b50505050905090810190601f1680156102755780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561028f57600080fd5b506102986105b8565b604051808060200180602001838103835285818151815260200191508051906020019080838360005b838110156102dc5780820151818401526020810190506102c1565b50505050905090810190601f1680156103095780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b83811015610342578082015181840152602081019050610327565b50505050905090810190601f16801561036f5780820380516001836020036101000a031916815260200191505b5094505050505060405180910390f35b600360019054906101000a900460ff16156103e15780600290805190602001906103aa92919061079b565b506000600360016101000a81548160ff02191690831515021790555060016003806101000a81548160ff0219169083151502179055505b50565b6000600360029054906101000a900460ff16905090565b60006003809054906101000a900460ff16905090565b600360009054906101000a900460ff161561048b57816001908051906020019061043c92919061079b565b50806000908051906020019061045392919061079b565b506000600360006101000a81548160ff0219169083151502179055506001600360026101000a81548160ff0219169083151502179055505b5050565b60606003809054906101000a900460ff161561057c576001600360006101000a81548160ff02191690831515021790555060006003806101000a81548160ff02191690831515021790555060028054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156105705780601f1061054557610100808354040283529160200191610570565b820191906000526020600020905b81548152906001019060200180831161055357829003601f168201915b505050505090506105b5565b6040805190810160405280600c81526020017f706c65617365207761697421000000000000000000000000000000000000000081525090505b90565b606080600360029054906101000a900460ff161561074b576001600360016101000a81548160ff0219169083151502179055506000600360026101000a81548160ff02191690831515021790555060006001818054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561069f5780601f106106745761010080835404028352916020019161069f565b820191906000526020600020905b81548152906001019060200180831161068257829003601f168201915b50505050509150808054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561073b5780601f106107105761010080835404028352916020019161073b565b820191906000526020600020905b81548152906001019060200180831161071e57829003601f168201915b5050505050905091509150610797565b6040805190810160405280600b81526020017f706c6561736520776169740000000000000000000000000000000000000000008152506020604051908101604052806000815250915091505b9091565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106107dc57805160ff191683800117855561080a565b8280016001018555821561080a579182015b828111156108095782518255916020019190600101906107ee565b5b509050610817919061081b565b5090565b61083d91905b80821115610839576000816000905550600101610821565b5090565b905600a165627a7a72305820ef08c922710b58a8df849f8e13e256a0c4ae4f8580634ebf91af56e9ce7920ba0029"};

    public static final String BINARY = org.fisco.bcos.sdk.utils.StringUtils.joinAll("", BINARY_ARRAY);

    public static final String[] SM_BINARY_ARRAY = {"60806040526001600360006101000a81548160ff0219169083151502179055506000600360016101000a81548160ff0219169083151502179055506000600360026101000a81548160ff02191690831515021790555060006003806101000a81548160ff02191690831515021790555034801561007b57600080fd5b5061086c8061008b6000396000f300608060405260043610610078576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306b957551461007d5780635d126208146100ac5780637ca3054214610115578063a069ec8e146101c4578063c0df30cf146102c0578063cf95d185146102ef575b600080fd5b34801561008957600080fd5b5061009261037f565b604051808215151515815260200191505060405180910390f35b3480156100b857600080fd5b50610113600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290505050610396565b005b34801561012157600080fd5b506101c2600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506103fb565b005b3480156101d057600080fd5b506101d9610479565b604051808060200180602001838103835285818151815260200191508051906020019080838360005b8381101561021d578082015181840152602081019050610202565b50505050905090810190601f16801561024a5780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b83811015610283578082015181840152602081019050610268565b50505050905090810190601f1680156102b05780820380516001836020036101000a031916815260200191505b5094505050505060405180910390f35b3480156102cc57600080fd5b506102d561065c565b604051808215151515815260200191505060405180910390f35b3480156102fb57600080fd5b50610304610672565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610344578082015181840152602081019050610329565b50505050905090810190601f1680156103715780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000600360029054906101000a900460ff16905090565b600360019054906101000a900460ff16156103f85780600290805190602001906103c192919061079b565b506000600360016101000a81548160ff02191690831515021790555060016003806101000a81548160ff0219169083151502179055505b50565b600360009054906101000a900460ff161561047557816001908051906020019061042692919061079b565b50806000908051906020019061043d92919061079b565b506000600360006101000a81548160ff0219169083151502179055506001600360026101000a81548160ff0219169083151502179055505b5050565b606080600360029054906101000a900460ff161561060c576001600360016101000a81548160ff0219169083151502179055506000600360026101000a81548160ff02191690831515021790555060006001818054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156105605780601f1061053557610100808354040283529160200191610560565b820191906000526020600020905b81548152906001019060200180831161054357829003601f168201915b50505050509150808054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156105fc5780601f106105d1576101008083540402835291602001916105fc565b820191906000526020600020905b8154815290600101906020018083116105df57829003601f168201915b5050505050905091509150610658565b6040805190810160405280600b81526020017f706c6561736520776169740000000000000000000000000000000000000000008152506020604051908101604052806000815250915091505b9091565b60006003809054906101000a900460ff16905090565b60606003809054906101000a900460ff161561075f576001600360006101000a81548160ff02191690831515021790555060006003806101000a81548160ff02191690831515021790555060028054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156107535780601f1061072857610100808354040283529160200191610753565b820191906000526020600020905b81548152906001019060200180831161073657829003601f168201915b50505050509050610798565b6040805190810160405280600c81526020017f706c65617365207761697421000000000000000000000000000000000000000081525090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106107dc57805160ff191683800117855561080a565b8280016001018555821561080a579182015b828111156108095782518255916020019190600101906107ee565b5b509050610817919061081b565b5090565b61083d91905b80821115610839576000816000905550600101610821565b5090565b905600a165627a7a72305820c3eaf153fd3510076140bc18508f54bb904bae1fa52d3778e6fbaacddb0737960029"};

    public static final String SM_BINARY = org.fisco.bcos.sdk.utils.StringUtils.joinAll("", SM_BINARY_ARRAY);

    public static final String[] ABI_ARRAY = {"[{\"constant\":false,\"inputs\":[{\"name\":\"backhashvalue\",\"type\":\"string\"}],\"name\":\"Serversend\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"server_watch_client\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"client_watch_server\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"wm_hashvalue\",\"type\":\"string\"},{\"name\":\"im_hashvalue\",\"type\":\"string\"}],\"name\":\"Clientsend\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"clientget\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"serverget\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"},{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]"};

    public static final String ABI = org.fisco.bcos.sdk.utils.StringUtils.joinAll("", ABI_ARRAY);

    public static final String FUNC_SERVERSEND = "Serversend";

    public static final String FUNC_SERVER_WATCH_CLIENT = "server_watch_client";

    public static final String FUNC_CLIENT_WATCH_SERVER = "client_watch_server";

    public static final String FUNC_CLIENTSEND = "Clientsend";

    public static final String FUNC_CLIENTGET = "clientget";

    public static final String FUNC_SERVERGET = "serverget";

    protected Transhash(String contractAddress, Client client, CryptoKeyPair credential) {
        super(getBinary(client.getCryptoSuite()), contractAddress, client, credential);
    }

    public static String getBinary(CryptoSuite cryptoSuite) {
        return (cryptoSuite.getCryptoTypeConfig() == CryptoType.ECDSA_TYPE ? BINARY : SM_BINARY);
    }

    public TransactionReceipt Serversend(String backhashvalue) {
        final Function function = new Function(
                FUNC_SERVERSEND, 
                Arrays.<Type>asList(new Utf8String(backhashvalue)),
                Collections.<TypeReference<?>>emptyList());
        return executeTransaction(function);
    }

    public void Serversend(String backhashvalue, TransactionCallback callback) {
        final Function function = new Function(
                FUNC_SERVERSEND, 
                Arrays.<Type>asList(new Utf8String(backhashvalue)),
                Collections.<TypeReference<?>>emptyList());
        asyncExecuteTransaction(function, callback);
    }

    public String getSignedTransactionForServersend(String backhashvalue) {
        final Function function = new Function(
                FUNC_SERVERSEND, 
                Arrays.<Type>asList(new Utf8String(backhashvalue)),
                Collections.<TypeReference<?>>emptyList());
        return createSignedTransaction(function);
    }

    public Tuple1<String> getServersendInput(TransactionReceipt transactionReceipt) {
        String data = transactionReceipt.getInput().substring(10);
        final Function function = new Function(FUNC_SERVERSEND, 
                Arrays.<Type>asList(), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
        List<Type> results = FunctionReturnDecoder.decode(data, function.getOutputParameters());
        return new Tuple1<String>(

                (String) results.get(0).getValue()
                );
    }

    public Boolean server_watch_client() throws ContractException {
        final Function function = new Function(FUNC_SERVER_WATCH_CLIENT, 
                Arrays.<Type>asList(), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {}));
        return executeCallWithSingleValueReturn(function, Boolean.class);
    }

    public Boolean client_watch_server() throws ContractException {
        final Function function = new Function(FUNC_CLIENT_WATCH_SERVER, 
                Arrays.<Type>asList(), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {}));
        return executeCallWithSingleValueReturn(function, Boolean.class);
    }

    public TransactionReceipt Clientsend(String wm_hashvalue, String im_hashvalue) {
        final Function function = new Function(
                FUNC_CLIENTSEND, 
                Arrays.<Type>asList(new Utf8String(wm_hashvalue),
                new Utf8String(im_hashvalue)),
                Collections.<TypeReference<?>>emptyList());
        return executeTransaction(function);
    }

    public void Clientsend(String wm_hashvalue, String im_hashvalue, TransactionCallback callback) {
        final Function function = new Function(
                FUNC_CLIENTSEND, 
                Arrays.<Type>asList(new Utf8String(wm_hashvalue),
                new Utf8String(im_hashvalue)),
                Collections.<TypeReference<?>>emptyList());
        asyncExecuteTransaction(function, callback);
    }

    public String getSignedTransactionForClientsend(String wm_hashvalue, String im_hashvalue) {
        final Function function = new Function(
                FUNC_CLIENTSEND, 
                Arrays.<Type>asList(new Utf8String(wm_hashvalue),
                new Utf8String(im_hashvalue)),
                Collections.<TypeReference<?>>emptyList());
        return createSignedTransaction(function);
    }

    public Tuple2<String, String> getClientsendInput(TransactionReceipt transactionReceipt) {
        String data = transactionReceipt.getInput().substring(10);
        final Function function = new Function(FUNC_CLIENTSEND, 
                Arrays.<Type>asList(), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}, new TypeReference<Utf8String>() {}));
        List<Type> results = FunctionReturnDecoder.decode(data, function.getOutputParameters());
        return new Tuple2<String, String>(

                (String) results.get(0).getValue(), 
                (String) results.get(1).getValue()
                );
    }

    public String clientget() throws ContractException {
        final Function function = new Function(FUNC_CLIENTGET, 
                Arrays.<Type>asList(), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
        return executeCallWithSingleValueReturn(function, String.class);
    }

    public Tuple2<String, String> serverget() throws ContractException {
        final Function function = new Function(FUNC_SERVERGET, 
                Arrays.<Type>asList(), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}, new TypeReference<Utf8String>() {}));
        List<Type> results = executeCallWithMultipleValueReturn(function);
        return new Tuple2<String, String>(
                (String) results.get(0).getValue(), 
                (String) results.get(1).getValue());
    }

    public static Transhash load(String contractAddress, Client client, CryptoKeyPair credential) {
        return new Transhash(contractAddress, client, credential);
    }

    public static Transhash deploy(Client client, CryptoKeyPair credential) throws ContractException {
        return deploy(Transhash.class, client, credential, getBinary(client.getCryptoSuite()), "");
    }
}